package com.ktwlrj.core.util.xpoi;

import com.ktwlrj.core.util.xpoi.model.DataSourceField;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 根据特定Excel导出Excel文档（仅执行单工作簿的excel模板）
 *
 * @author zhouxuanhong
 * @date 2019-08-02 12:14
 * @since JDK1.8
 */
public class SimpleWorkbookDesigner extends BaseWorkbook {

    @Override
    public void process() throws IOException {
        // 获取第一张工作簿
        Sheet sheet = this.getSheet(0);

        // 工作簿自定义字段集
        Map<String, DataSourceField> cdsField = new HashMap<>(16);

        // 工作簿数据源字段集
        Map<String, DataSourceField> dsField = new HashMap<>(16);

        // 读取工作簿自定义字段，数据源字段数据集
        this.getDataSourceField(sheet, cdsField, dsField);

        // 写自定义字段
        if (!CollectionUtils.isEmpty(cdsField)) {
            this.writeCustomField(sheet, cdsField, dataSource);
        }

        // 写数据源
        if (!CollectionUtils.isEmpty(dsField)) {
            this.writeDataSource(sheet, dsField, dataSource);
        }

        this.workBook.write(outStream);
        this.workBook.close();
    }
}
